open(BUFF,"Gene_start_stop.txt");
open(BUFF1,"/data1/raw/ngs/illumina-ga-1/bioinf_int/MayoGAP/mayogap_emerge/clean/beagle/run_beagle/beagle_mainrun/snps_hapmap3_cord_chr.txt");
$n =0;
$line = <BUFF>;

while($line = <BUFF>)
{
	chomp($line);
	$line =~ s/\n//g;
	$line =~ s/\r//g;
 	@array = split(/\t/,$line);
	$array[0] =~ s/\s//g;
	$array[1] =~ s/\s//g;
	$array[2] =~ s/\s//g;
	$gene[$n] =$array[0];
	$start[$n] =$array[1];	
	$stop[$n] =$array[2];
	$n++
}
@index = sort{ $stop[ $a ] <=> $stop[ $b ] } 0 .. $#stop;
 #@start1 = sort {$a <=> $b} @start;
for($i=0;$i<@gene;$i++)
{
	$gene1[$i] = $gene[$index[$i]];
	$start1[$i] = $start[$index[$i]];
	
	$stop1[$i] = $stop[$index[$i]];
	if($start1[$i] > 10000)
	{
		$start1[$i] = $start1[$i] - 10000
	}
	$stop1[$i] = $stop1[$i] + 10000;
	$num{$gene1[$i]} =0;
	
}
$n=0;
$n1 = 1000000;
$n2 = 0;
for($i=0;$i<@gene1;$i=$i++)
{
	if($stop1[$i] <$n1)
	{
		${"array".$n2}[$n] = $i;

	}
	else
	{
		$n2++;
		$n =0;
		${"array".$n2}[$n] = $i;
	}

}
$n =0;
while($line = <BUFF1>)
{
	chomp($line);
	@array = split(/\t/,$line);
	$rs[$n] = $array[0];
	$pos[$n] = $array[1];
	$n++;
}
@index1 = sort{ $pos[ $a ] <=> $pos[ $b ] } 0 .. $#pos;
for($i=0;$i<@pos;$i++)
{
	$pos1[$i] = $pos[$index1[$i]];	
	$rs1[$i] = $rs[$index1[$i]];
}
$n=0;
$n1 = 1000000;
$n3 = 0;
for($i=0;$i<@pos1;$i=$i++)
{
        if($stop1[$i] <$n1)
        {
                ${"sarray".$n3}[$n] = $i;

        }
        else
        {
                $n3++;
                $n =0;
                ${"sarray".$n3}[$n] = $i;
        }

}

print "$n2\t$n3\n";
